import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component:()=>import('@/views/Home.vue'),
      redirect:'/Index',
      children:[
        {
          path:'Index',
          name:'index',
          component:()=>import('@/views/Index.vue')
        },
        {
          path:'Classify',
          name:'classify',
          component:()=>import('@/views/Classify.vue')
        },
        {
          path:'ShoppingCart',
          name:'shoppingcart',
          component:()=>import('@/views/ShoppingCart.vue'),
          meta:{
            requireAuth:true
          }
        },
        {
          path:'My',
          name:'my',
          component:()=>import('@/views/My.vue')
        }
      ]
    },
    {
      path:'/Login',
      name:'login',
      component:()=>import('@/views/Login.vue'),
    },
    {
      path:'/Register',
      name:'register',
      component:()=>import('@/views/Register.vue')
    },
    {
      path:'/ProductInfo',
      name:'productinfo',
      component:()=>import('@/views/ProductInfo.vue')
    },
    {
      path:'/AddressList',
      name:'addresslist',
      component:()=>import('@/views/AddressList.vue')
    },
    {
      path:'/AddressEdit',
      name:'addressedit',
      component:()=>import('@/views/AddressEdit.vue')
    },
    {
      path:'/Order',
      name:'order',
      component:()=>import('@/views/Order.vue')
    },
    {
      path:'/Recharge',
      name:'recharge',
      component:()=>import('@/views/Recharge.vue')
    },
    {
      path:'/MyOrder',
      name:'myorder',
      component:()=>import('@/views/MyOrder.vue'),
      meta:{
        requireAuth:true
      }
    },
    {
      path:'/Search',
      name:'search',
      component:()=>import('@/views/Search.vue'),
    },
    {
      path:'/SearchInfo',
      name:'searchinfo',
      component:()=>import('@/views/SearchInfo.vue'),
    },
  ]
})
router.beforeEach((to,from,)=>{
  const token=localStorage.getItem('token')
  if(!token&&to.meta.requireAuth){
    return '/login'
  }
})
export default router
